/*
 * @Author: wuzhen
 * @Date: 2019-12-31 14:01:22
 * @LastEditors  : wuzhen
 * @LastEditTime : 2020-01-08 11:07:53
 * @Description: 权限配置
 */
import { getQryoption } from "@/request/api";
export default {
  data() {
    return {
      dataAuthority: []
    };
  },
  computed: {
    columnsAuthority() {
      const data = this.$store.state.user["actions"].map(v => {
        return {
          title: v.actionname,
          width: 80,
          align: "center",
          render: (h, params) => {
            const row = params.row;
            row.isCheck = false;
            const optionid = String(row.moduleid) + String(v.actionid);
            return h("Checkbox", {
              props: {
                value: row.optionids.includes(optionid) || row.isCheckAll
              },
              on: {
                "on-change": e => {
                  if (!e) {
                    row.isCheckAll = false;
                  }
                  this.optionids = this.$_.difference(this.optionids, [
                    optionid
                  ]);
                  if (e) {
                    this.optionids = [...this.optionids, ...[optionid]];
                  }
                  row.optionids = this.optionids;
                  row.isCheckAll =
                    this.optionids.filter(
                      m => m.slice(0, 4) === String(row.moduleid)
                    ).length === this.$store.state.user["actions"].length;
                }
              }
            });
          }
        };
      });
      data.unshift({
        title: "模块名称",
        render: (h, params) => {
          const row = params.row;
          return h(
            "Checkbox",
            {
              props: {
                value: row.isCheckAll
              },
              on: {
                "on-change": e => {
                  row.isCheckAll = e;
                  const optionids = [];
                  this.$store.state.user["actions"].forEach(v => {
                    optionids.push(String(row.moduleid) + String(v.actionid));
                  });
                  this.optionids = this.$_.difference(
                    this.optionids,
                    optionids
                  );
                  if (e) {
                    this.optionids = [...this.optionids, ...optionids];
                  }
                  row.optionids = this.optionids;
                }
              }
            },
            row.modulename
          );
        }
      });
      return data;
    }
  },
  methods: {
    async getQryoptionData(roleid) {
      try {
        this.qryoptionData = [];
        this.optionids = [];
        const res = await getQryoption({ bean: { roleid: roleid } });
        this.qryoptionData = res.result;
        this.optionids = res.result.map(v => String(v.optionid));
        this.basicOptionids = this.optionids;
        this.dataAuthority = this.$store.state.user["modules"].map(v => {
          return {
            ...v,
            ...{
              isCheckAll:
                this.optionids.filter(m => m.slice(0, 4) === String(v.moduleid))
                  .length === this.$store.state.user["actions"].length,
              optionids: this.optionids
            }
          };
        });
        this.authorityDrawer = true;
      } catch (error) {
        throw error;
      }
    }
  }
};
